﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using System.Security.Cryptography;

namespace NS
{
    [HotUpdate]
    [Description("组柜登记单审核时回写采购订单")]
    public class Registration : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            string FID = "", FZGID = "";
            foreach (DynamicObject item in e.DataEntitys)
            {
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FID = Convert.ToString(item["ID"]);
                }
                //判断是否关联采购订单、收料通知单
                string SQL = "/*dialect*/" + string.Format(@"select t4.FBILLNO,t7.FBILLNO,T7.FZGID,t7.F_WNEG_Text from QZPV_t_Cust100001 t
                left join QZPV_t_Cust_Entry100001 t1 on t.fid=t1.fid
                left join QZPV_t_Cust_Entry100001_LK t2 on t1.FEntryID=t2.FEntryID
                left join T_PUR_ReceiveEntry t3 on t2.FSBillId=t3.FID and t2.FSId=t3.FENTRYID
                left join T_PUR_Receive t4 on t3.fid=t4.fid
                left join T_PUR_ReceiveEntry_lk t5 on t3.FENTRYID=t5.FENTRYID
                left join t_PUR_POOrderEntry t6 on t5.FSBillId=t6.fid and t5.FSID=t6.FENTRYID
                left join t_PUR_POOrder t7 on t6.fid=t7.fid
                where t7.FBILLNO is not null and t.FID='{0}' ", FID);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                if (DynamData.Count > 0)
                {
                    for (int a = 0; a < DynamData.Count; a++)
                    {


                        FZGID = Convert.ToString(DynamData[a]["F_WNEG_Text"]);
                        if (FZGID.Equals("") || FZGID.Equals(" "))
                        {
                            string SQL01 = "/*dialect*/" + string.Format(@"update t set 
                    t.FZGID=t7.fid
                    from t_PUR_POOrder t
                    left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                    left join T_PUR_ReceiveEntry_lk t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                    left join T_PUR_ReceiveEntry t3 on t2.FEntryID=t3.FEntryID
                    left join T_PUR_Receive t4 on t3.fid=t4.fid
                    left join QZPV_t_Cust_Entry100001_LK t5 on t3.fid=t5.FSBillId and t3.FEntryID=t5.fsid
                    left join QZPV_t_Cust_Entry100001 t6 on t5.FEntryID=t6.FEntryID
                    left join QZPV_t_Cust100001 t7 on t6.fid=t7.fid
                    where t7.fid='{0}' 


", FID, Convert.ToString(DateTime.Now));
                            DBUtils.ExecuteDynamicObject(this.Context, SQL01);
                        }
                        else
                        {
                            string SQL0 = "/*dialect*/" + string.Format(@"update t set 
                    t.F_WLLZ_Date2=t7.F_WLLZ_Date11,
                    t.F_WLLZ_Date1=t7.F_WLLZ_Date111,
                    t.F_WLLZ_Date4=t7.F_WLLZ_Date12,
                    t.F_WLLZ_Date5=t7.F_WLLZ_Date13,
                    t.F_WLLZ_Date3=t7.F_WLLZ_Date14,
                    t.F_WNEG_Remarks5=t7.F_WNEG_REMARKS1,
                    t.FModifyDate='{1}'
                    from t_PUR_POOrder t
                    left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                    left join T_PUR_ReceiveEntry_lk t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                    left join T_PUR_ReceiveEntry t3 on t2.FEntryID=t3.FEntryID
                    left join T_PUR_Receive t4 on t3.fid=t4.fid
                    left join QZPV_t_Cust_Entry100001_LK t5 on t3.fid=t5.FSBillId and t3.FEntryID=t5.fsid
                    left join QZPV_t_Cust_Entry100001 t6 on t5.FEntryID=t6.FEntryID
                    left join QZPV_t_Cust100001 t7 on t6.fid=t7.fid
                    where  t7.fid='{0}' ", FID, Convert.ToString(DateTime.Now));
                            DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                        }
                        string SQL3 = "/*dialect*/" + string.Format(@"update t set 
                    t.F_WLLZ_Date2=t7.F_WLLZ_Date11,
                    t.F_WLLZ_Date1=t7.F_WLLZ_Date111,
                    t.F_WLLZ_Date4=t7.F_WLLZ_Date12,
                    t.F_WLLZ_Date5=t7.F_WLLZ_Date13,
                    t.F_WLLZ_Date3=t7.F_WLLZ_Date14,
                    t.F_WNEG_Remarks5=t7.F_WNEG_REMARKS1,
                    t.FModifyDate='{1}'
                    from t_PUR_POOrder t
                    left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                    left join T_PUR_ReceiveEntry_lk t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                    left join T_PUR_ReceiveEntry t3 on t2.FEntryID=t3.FEntryID
                    left join T_PUR_Receive t4 on t3.fid=t4.fid
                    left join QZPV_t_Cust_Entry100001_LK t5 on t3.fid=t5.FSBillId and t3.FEntryID=t5.fsid
                    left join QZPV_t_Cust_Entry100001 t6 on t5.FEntryID=t6.FEntryID
                    left join QZPV_t_Cust100001 t7 on t6.fid=t7.fid
                    where  t7.fid='{0}' ", FID, Convert.ToString(DateTime.Now));
                        DBUtils.ExecuteDynamicObject(this.Context, SQL3);
                        string SQL4 = "/*dialect*/" + string.Format(@"select distinct t.fid 
into #A
from t_PUR_POOrder t
                    left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                    left join T_PUR_ReceiveEntry_lk t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                    left join T_PUR_ReceiveEntry t3 on t2.FEntryID=t3.FEntryID
                    left join T_PUR_Receive t4 on t3.fid=t4.fid
                    left join QZPV_t_Cust_Entry100001_LK t5 on t3.fid=t5.FSBillId and t3.FEntryID=t5.fsid
                    left join QZPV_t_Cust_Entry100001 t6 on t5.FEntryID=t6.FEntryID
                    left join QZPV_t_Cust100001 t7 on t6.fid=t7.fid
					where t7.F_WLLZ_Text12 is not null AND t7.fid='{0}' 
					--and t7.FID=100306
					order by t.FID

SELECT * INTO #B FROM (
					select distinct t.fid CGFID,T7.FID,t7.F_WLLZ_Text12
                    from t_PUR_POOrder t
                    left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                    left join T_PUR_ReceiveEntry_lk t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                    left join T_PUR_ReceiveEntry t3 on t2.FEntryID=t3.FEntryID
                    left join T_PUR_Receive t4 on t3.fid=t4.fid
                    left join QZPV_t_Cust_Entry100001_LK t5 on t3.fid=t5.FSBillId and t3.FEntryID=t5.fsid
                    left join QZPV_t_Cust_Entry100001 t6 on t5.FEntryID=t6.FEntryID
                    left join QZPV_t_Cust100001 t7 on t6.fid=t7.fid
					where t7.F_WLLZ_Text12 is not null 
					and t.FID in
					(select * from #A)
					union all

                        SELECT distinct t.fid CGFID,T4.FID,t4.F_WLLZ_Text12
                        from t_PUR_POOrder t
                        left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                        left join QZPV_t_Cust_Entry100001_LK t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                        left join QZPV_t_Cust_Entry100001 t3 on t2.FEntryID=t3.FEntryID
                        left join QZPV_t_Cust100001 t4 on t3.fid=t4.fid
                        where t.fid in (select * from #A)
)T

			
	SELECT t.CGFID,
       STUFF((SELECT ',' + t1.F_WLLZ_Text12
              FROM (
			  SELECT CGFID, F_WLLZ_Text12 FROM #B WHERE CGFID = t.CGFID 
			  ) AS t1 FOR XML PATH('')), 1, 1, '')F_WLLZ_Text12
			  into #C
FROM (SELECT DISTINCT CGFID FROM #B)  t;

update t set t.F_WNEG_Text=T1.F_WLLZ_Text12 from t_PUR_POOrder t
LEFT JOIN #C T1 ON T.FID=T1.CGFID
WHERE T1.F_WLLZ_Text12 IS NOT NULL ", FID);
                        DBUtils.ExecuteDynamicObject(this.Context, SQL4);
                    }
                }
                //判断是否直接关联采购订单
                string SQL1 = "/*dialect*/" + string.Format(@"select t4.FBILLNO,t4.FZGID,t4.F_WNEG_Text from QZPV_t_Cust100001 t
                left join QZPV_t_Cust_Entry100001 t1 on t.fid=t1.fid
                left join QZPV_t_Cust_Entry100001_LK t2 on t1.FEntryID=t2.FEntryID
                left join t_PUR_POOrderEntry t3 on t2.FSBillId=t3.FID and t2.FSId=t3.FENTRYID
                left join t_PUR_POOrder t4 on t3.fid=t4.fid
                where t4.FBILLNO is not null and t.FID='{0}' ", FID);
                DynamicObjectCollection DynamData1 = DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                if (DynamData1.Count > 0)
                {
                    for (int a = 0; a < DynamData1.Count; a++)
                    {
                        FZGID = Convert.ToString(DynamData1[a]["F_WNEG_Text"]);

                        if (FZGID.Equals("") || FZGID.Equals(" "))
                        {
                            string SQL20 = "/*dialect*/" + string.Format(@"update t set 
                        t.FZGID=t4.fid
                        from t_PUR_POOrder t
                        left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                        left join QZPV_t_Cust_Entry100001_LK t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                        left join QZPV_t_Cust_Entry100001 t3 on t2.FEntryID=t3.FEntryID
                        left join QZPV_t_Cust100001 t4 on t3.fid=t4.fid
                        where  t4.fid='{0}'



", FID, Convert.ToString(DateTime.Now));
                            DBUtils.ExecuteDynamicObject(this.Context, SQL20);
                        }

                        else
                        {
                            string SQL2 = "/*dialect*/" + string.Format(@"update t set 
                        t.F_WLLZ_Date2=t4.F_WLLZ_Date11,
                        t.F_WLLZ_Date1=t4.F_WLLZ_Date111,
                        t.F_WLLZ_Date4=t4.F_WLLZ_Date12,
                        t.F_WLLZ_Date5=t4.F_WLLZ_Date13,
                        t.F_WLLZ_Date3=t4.F_WLLZ_Date14,
                        t.F_WNEG_Remarks5=t4.F_WNEG_REMARKS1,
                        t.FModifyDate='{1}'
                        from t_PUR_POOrder t
                        left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                        left join QZPV_t_Cust_Entry100001_LK t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                        left join QZPV_t_Cust_Entry100001 t3 on t2.FEntryID=t3.FEntryID
                        left join QZPV_t_Cust100001 t4 on t3.fid=t4.fid
                        where  t4.fid='{0}'", FID, Convert.ToString(DateTime.Now));
                            DBUtils.ExecuteDynamicObject(this.Context, SQL2);
                        }
                        string SQL3 = "/*dialect*/" + string.Format(@"SELECT distinct t.fid 
INTO #A
                        from t_PUR_POOrder t
                        left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                        left join QZPV_t_Cust_Entry100001_LK t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                        left join QZPV_t_Cust_Entry100001 t3 on t2.FEntryID=t3.FEntryID
                        left join QZPV_t_Cust100001 t4 on t3.fid=t4.fid
where t4.fid='{0}'

	SELECT * INTO #B FROM (
					select distinct t.fid CGFID,T7.FID,t7.F_WLLZ_Text12
                    from t_PUR_POOrder t
                    left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                    left join T_PUR_ReceiveEntry_lk t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                    left join T_PUR_ReceiveEntry t3 on t2.FEntryID=t3.FEntryID
                    left join T_PUR_Receive t4 on t3.fid=t4.fid
                    left join QZPV_t_Cust_Entry100001_LK t5 on t3.fid=t5.FSBillId and t3.FEntryID=t5.fsid
                    left join QZPV_t_Cust_Entry100001 t6 on t5.FEntryID=t6.FEntryID
                    left join QZPV_t_Cust100001 t7 on t6.fid=t7.fid
					where t7.F_WLLZ_Text12 is not null 
					and t.FID in
					(select * from #A)
					union all

                        SELECT distinct t.fid CGFID,T4.FID,t4.F_WLLZ_Text12
                        from t_PUR_POOrder t
                        left join t_PUR_POOrderEntry t1 on t.fid=t1.fid
                        left join QZPV_t_Cust_Entry100001_LK t2 on t1.fid=t2.FSBillId and t1.FEntryID=t2.fsid
                        left join QZPV_t_Cust_Entry100001 t3 on t2.FEntryID=t3.FEntryID
                        left join QZPV_t_Cust100001 t4 on t3.fid=t4.fid
                        where t.fid in (select * from #A)
)T

						SELECT t.CGFID,
       STUFF((SELECT ',' + t1.F_WLLZ_Text12
              FROM (
			  SELECT CGFID, F_WLLZ_Text12 FROM #B WHERE CGFID = t.CGFID 
			  ) AS t1 FOR XML PATH('')), 1, 1, '')F_WLLZ_Text12
			  into #C
FROM (SELECT DISTINCT CGFID FROM #B)  t;

update t set t.F_WNEG_Text=T1.F_WLLZ_Text12 from t_PUR_POOrder t
LEFT JOIN #C T1 ON T.FID=T1.CGFID
WHERE T1.F_WLLZ_Text12 IS NOT NULL ", FID, Convert.ToString(DateTime.Now));
                        DBUtils.ExecuteDynamicObject(this.Context, SQL3);
                    }
                }
            }
        }
    }
    [HotUpdate]
    [Description("组柜登记单保存时回写采购订单、组柜登记单")]
    public class ZGDJ : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }

        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            string FID = "";
            foreach (DynamicObject item in e.DataEntitys)
            {
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FID = Convert.ToString(item["ID"]);
                }
                string SQL0 = "/*dialect*/" + string.Format(@"select * from t_PUR_POOrderEntry t
                INNER join QZPV_t_Cust_Entry100001_lk t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                inner join QZPV_t_Cust_Entry100001 t2 on t1.fentryid=t2.fentryid
                where t2.fid='{0}'", FID);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                string SQL1 = "/*dialect*/" + string.Format(@"select * from t_PUR_POOrderEntry t
                INNER join T_PUR_RECEIVEENTRY_LK t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                inner join T_PUR_RECEIVEENTRY t2 on t1.fentryid=t2.fentryid
                inner join QZPV_t_Cust_Entry100001_lk t3 on t2.fid=t3.fsbillid and  t2.fentryid=t3.fsid
                inner join QZPV_t_Cust_Entry100001 t4 on t3.fentryid=t4.fentryid
                where t4.fid='{0}'", FID);
                DynamicObjectCollection DynamData1 = DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                if (DynamData.Count > 0|| DynamData1.Count>0)
                {
                    string SQL = "/*dialect*/" + string.Format(@"
                    					SELECT sum(FQTY)FQTY,FSId,FSBillId,FSTableName INTO #A FROM (
					select sum(t.FQTY)FQTY,t1.FSId,t1.FSBillId,t1.FSTableName  from QZPV_t_Cust_Entry100001 t
                    inner join QZPV_t_Cust_Entry100001_lk t1 on t.fentryid=t1.fentryid
                    where t1.FSId is not null 
                    group by t1.FSId,t1.FSBillId,t1.FSTableName 
					UNION ALL
					 select sum(t4.FQTY)FQTY,t1.FSId,t1.FSBillId,t1.FSTableName  from t_PUR_POOrderEntry t
                    INNER join T_PUR_RECEIVEENTRY_LK t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                    inner join T_PUR_RECEIVEENTRY t2 on t1.fentryid=t2.fentryid
                    inner join QZPV_t_Cust_Entry100001_lk t3 on t2.fid=t3.fsbillid and  t2.fentryid=t3.fsid
                    inner join QZPV_t_Cust_Entry100001 t4 on t3.fentryid=t4.fentryid
                    --where t4.fid='{0}'
                    group by t1.FSId,t1.FSBillId,t1.FSTableName 
					)T 
					GROUP BY FSId,FSBillId,FSTableName
                    update t set t.F_WNEG_QTY=t.FQTY-t1.FQTY,t.F_WNEG_QTY4=t1.FQTY from t_PUR_POOrderEntry t
                    inner join #A T1 ON T.FENTRYID=T1.FSId AND T.FID=T1.FSBillId
                    where t1.FSTableName='t_PUR_POOrderEntry'", FID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL);
                }

                if (DynamData1.Count > 0)
                {
                    //string SQL = "/*dialect*/" + string.Format(@"
                    //select sum(t4.FQTY)FQTY,t1.FSId,t1.FSBillId,t1.FSTableName into #A from t_PUR_POOrderEntry t
                    //INNER join T_PUR_RECEIVEENTRY_LK t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                    //inner join T_PUR_RECEIVEENTRY t2 on t1.fentryid=t2.fentryid
                    //inner join QZPV_t_Cust_Entry100001_lk t3 on t2.fid=t3.fsbillid and  t2.fentryid=t3.fsid
                    //inner join QZPV_t_Cust_Entry100001 t4 on t3.fentryid=t4.fentryid
                    //--where t4.fid='{0}'
                    //group by t1.FSId,t1.FSBillId,t1.FSTableName 
                   
                    //update t set t.F_WNEG_QTY=t.FQTY-t1.FQTY,t.F_WNEG_QTY4=t1.FQTY from t_PUR_POOrderEntry t
                    //inner join #A T1 ON T.FENTRYID=T1.FSId AND T.FID=T1.FSBillId
                    //where t1.FSTableName='t_PUR_POOrderEntry'", FID);
                    //DBUtils.ExecuteDynamicObject(this.Context, SQL);

                    string SQL2 = "/*dialect*/" + string.Format(@"
                    select sum(t.FQTY)FQTY,t1.fsid,t1.FSBillId into #B from QZPV_t_Cust_Entry100001 t
					left join QZPV_t_Cust_Entry100001_lk t1 on t.fentryid=t1.fentryid
					group by t1.fsid,t1.FSBillId,t.FMATERIALID

					update t set t.F_WNEG_DECIMAL2=0 ,t.F_WNEG_QTY4=0  from T_PUR_RECEIVEENTRY t
                    left join QZPV_t_Cust_Entry100001_lk t1 on t.FID=t1.FSBillId and t.FENTRYID=t1.FSId
					where t1.FSTableName='T_PUR_ReceiveEntry'

					update t set t.F_WNEG_DECIMAL2=t.FACTRECEIVEQTY-t1.fqty,t.F_WNEG_QTY4=t1.fqty  from T_PUR_RECEIVEENTRY t
                    inner join #B t1 on t.FID=t1.FSBillId and t.FENTRYID=t1.FSId

					update t set t.F_WNEG_DECIMAL2=t.FACTRECEIVEQTY ,t.F_WNEG_QTY4=0  from T_PUR_RECEIVEENTRY t
                    where t.FID not in (select FSBillId from #B)", FID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL2);
                }
                string SQL3 = "/*dialect*/" + string.Format(@"select T.FENTRYID INTO #A from t_PUR_POOrderEntry t
                INNER join QZPV_t_Cust_Entry100001_lk t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                inner join QZPV_t_Cust_Entry100001 t2 on t1.fentryid=t2.fentryid
                select sum(t4.FQTY)FQTY,t1.FSId,t1.FSBillId,t1.FSTableName,T.FENTRYID into #B from t_PUR_POOrderEntry t
                    INNER join T_PUR_RECEIVEENTRY_LK t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                    inner join T_PUR_RECEIVEENTRY t2 on t1.fentryid=t2.fentryid
                    inner join QZPV_t_Cust_Entry100001_lk t3 on t2.fid=t3.fsbillid and  t2.fentryid=t3.fsid
                    inner join QZPV_t_Cust_Entry100001 t4 on t3.fentryid=t4.fentryid
                    --where t.fid=116254
                    group by t1.FSId,t1.FSBillId,t1.FSTableName ,T.FENTRYID
                    UPDATE T SET T.F_WNEG_QTY=t.FQTY,t.F_WNEG_QTY4=0 FROM t_PUR_POOrderEntry T
					WHERE T.FENTRYID NOT IN (SELECT FENTRYID FROM #B ) AND T.FENTRYID NOT IN (SELECT FENTRYID FROM #A)
");
                DBUtils.ExecuteDynamicObject(this.Context, SQL3);
            }
        }
    }
    [HotUpdate]
    [Description("组柜登记单删除时回写采购订单、组柜登记单")]
    public class DELETEZGDJ : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            base.OnPreparePropertys(e);
        }

        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            string FID = "";
            foreach (ExtendedDataEntity item in e.SelectedRows)
            {
                if (item["ID"] != null && Convert.ToString(item["ID"]) != "")
                {
                    FID = Convert.ToString(item["ID"]);
                }
                string SQL0 = "/*dialect*/" + string.Format(@"select * from t_PUR_POOrderEntry t
                INNER join QZPV_t_Cust_Entry100001_lk t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                inner join QZPV_t_Cust_Entry100001 t2 on t1.fentryid=t2.fentryid
                where t2.fid='{0}'", FID);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                if (DynamData.Count > 0)
                {
                    string SQL = "/*dialect*/" + string.Format(@"
                    update t set t.F_WNEG_QTY=t.F_WNEG_QTY+t2.fqty,t.F_WNEG_QTY4=t.F_WNEG_QTY4-t2.FQTY from t_PUR_POOrderEntry t
                    INNER join QZPV_t_Cust_Entry100001_lk t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                    inner join QZPV_t_Cust_Entry100001 t2 on t1.fentryid=t2.fentryid
                    where t2.fid='{0}'", FID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL);
                }
                string SQL1 = "/*dialect*/" + string.Format(@"select * from t_PUR_POOrderEntry t
                INNER join T_PUR_RECEIVEENTRY_LK t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                inner join T_PUR_RECEIVEENTRY t2 on t1.fentryid=t2.fentryid
                inner join QZPV_t_Cust_Entry100001_lk t3 on t2.fid=t3.fsbillid and  t2.fentryid=t3.fsid
                inner join QZPV_t_Cust_Entry100001 t4 on t3.fentryid=t4.fentryid
                where t4.fid='{0}'", FID);
                DynamicObjectCollection DynamData1 = DBUtils.ExecuteDynamicObject(this.Context, SQL1);
                if (DynamData1.Count > 0)
                {
                    string SQL = "/*dialect*/" + string.Format(@"
                    update t set t.F_WNEG_QTY=t.F_WNEG_QTY+t4.fqty,t.F_WNEG_QTY4=t.F_WNEG_QTY4-t4.FQTY from t_PUR_POOrderEntry t
                    INNER join T_PUR_RECEIVEENTRY_LK t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                    inner join T_PUR_RECEIVEENTRY t2 on t1.fentryid=t2.fentryid
                    inner join QZPV_t_Cust_Entry100001_lk t3 on t2.fid=t3.fsbillid and  t2.fentryid=t3.fsid
                    inner join QZPV_t_Cust_Entry100001 t4 on t3.fentryid=t4.fentryid
                    where t4.fid='{0}'", FID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL);
                    string SQL2 = "/*dialect*/" + string.Format(@"
                    update t set t.F_WNEG_DECIMAL2=t.F_WNEG_DECIMAL2+t2.fqty ,t.F_WNEG_QTY4=t.F_WNEG_QTY4-t2.fqty  from T_PUR_RECEIVEENTRY t
                    inner join QZPV_t_Cust_Entry100001_lk t1 on t.fid=t1.fsbillid and t.fentryid=t1.fsid
                    inner join QZPV_t_Cust_Entry100001 t2 on t1.fentryid=t2.fentryid
                    where t2.fid='{0}'", FID);
                    DBUtils.ExecuteDynamicObject(this.Context, SQL2);
                }
            }
        }
    }
    [HotUpdate]
    [Description("组柜登记单保存时检验未组柜数量是否为负数")]
    public class cheack : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e)
        {
            e.FieldKeys.Add("FID");
            e.FieldKeys.Add("FEntity");
            base.OnPreparePropertys(e);
        }
        public override void BeforeExecuteOperationTransaction(BeforeExecuteOperationTransaction e)
        {
            base.BeforeExecuteOperationTransaction(e);
            foreach (ExtendedDataEntity item in e.SelectedRows)
            {
                DynamicObjectCollection dataentrys = item["BillEntry"] as DynamicObjectCollection;
                foreach (DynamicObject entry in dataentrys)
                {
                    decimal F_WNEG_QTY4 = 0;
                    decimal FQty = 0;
                    decimal F_WLLZ_Qty = 0;
                    F_WLLZ_Qty = Convert.ToDecimal(entry["F_WLLZ_Qty"]);
                    DynamicObjectCollection FEntity_Link = entry["FEntity_Link"] as DynamicObjectCollection;
                    int SID = Convert.ToInt32(FEntity_Link[0]["SID"]);
                    string SQL0 = "/*dialect*/" + string.Format(@"select FQty,F_WNEG_QTY4 from t_PUR_POOrderEntry where FENTRYID='{0}'
                    ", SID);
                    DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL0);
                    if(DynamData.Count>0)
                    {
                        FQty = Convert.ToDecimal(DynamData[0]["FQty"]);
                        F_WNEG_QTY4 = Convert.ToDecimal(DynamData[0]["F_WNEG_QTY4"]);
                    }
                    if(FQty - F_WNEG_QTY4-F_WLLZ_Qty < 0)
                    {
                        throw new Exception("未组柜数量为负数！请检查订单组柜数量");
                    }
                }
            }
        }
    }
}